﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace WikiTrailCreator.TrailData
{
    /// <summary>
    /// Added this.
    /// </summary>
    public class Trail
    {
        // Main properties:
        public string TrailName = "";
        public string WikiCategory = "";

        // Sections:
        public SummarySection Summary = new SummarySection();
        public LandmarksSection Landmarks = new LandmarksSection();
        public MapsSection Maps = new MapsSection();
        public GettingThereSection GettingThere = new GettingThereSection();
        public PhotoGallerySection PhotoGallery = new PhotoGallerySection();
        public ConditionsSection Conditions = new ConditionsSection();
        public RelatedLinksSection RelatedLinks = new RelatedLinksSection();

        // All Trail Images:
        private List<LocalImage> trailImages = new List<LocalImage>();
        public LocalImage[] AllTrailImages
        {
            get
            {
                return trailImages.ToArray();
            }
            set
            {
                trailImages = new List<LocalImage>(value);
            }
        }

        // Add Trail Image
        public void AddTrailImage(LocalImage trailImage)
        {
            trailImages.Add(trailImage);
        }

        /// <summary>
        /// Saves the data to an XML file.
        /// </summary>
        /// <param name="fileName">The file path to save to.</param>
        public static void Save(string fileName, Trail trail)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Trail));
            FileStream fileStream = new FileStream(fileName, FileMode.Create);
            XmlTextWriter xmlTextWriter = new XmlTextWriter(fileStream, Encoding.Unicode);
            xmlSerializer.Serialize(fileStream, trail);
            fileStream.Close();
        }

        public static Trail Load(string fileName)
        {
            // Try this commento!
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Trail));
            FileStream fileStream = new FileStream(fileName, FileMode.Open);
            XmlTextReader xmlTextReader = new XmlTextReader(fileStream);
            xmlTextReader.Normalization = false;
            xmlTextReader.WhitespaceHandling = WhitespaceHandling.Significant;
            Trail loadedTrail = xmlSerializer.Deserialize(xmlTextReader) as Trail;
            xmlTextReader.Close();
            fileStream.Close();

            return loadedTrail;
        }
    }

    public class SummarySection
    {
        public string Difficulty = "";
        public string Length = "";
        public string NumDaysToHike = "";
        public string NumHoursToHike = "";
        public string Snapshot = "";
        public string Navigation = "";
        public string Shelter = "";
        public string Season = "";
        public string Costs = "";

        // Dangers
        private List<Danger> dangers = new List<Danger>();
        public Danger[] DangersOrAnnoyances
        {
            get
            {
                return dangers.ToArray();
            }
            set
            {
                dangers = new List<Danger>(value);
            }
        }

        // Tips
        private List<Tip> tips = new List<Tip>();
        public Tip[] Tips
        {
            get
            {
                return tips.ToArray();
            }
            set
            {
                tips = new List<Tip>(value);
            }
        }

        // Add danger
        public void AddDanger(Danger danger)
        {
            dangers.Add(danger);
        }

        // Add tip
        public void AddTip(Tip tip)
        {
            tips.Add(tip);
        }
    }

    public class Danger
    {
        public string Name = "";
        public string Description = "";
    }

    public class Tip
    {
        public string TipText = "";
    }

    public class LandmarksSection
    {
        // Landmarks
        private List<Landmark> landmarks = new List<Landmark>();
        public Landmark[] Landmarks
        {
            get
            {
                return landmarks.ToArray();
            }
            set
            {
                landmarks = new List<Landmark>(value);
            }
        }

        // Add landmark
        public void AddLandmark(Landmark landmark)
        {
            landmarks.Add(landmark);
        }
    }

    public class Landmark
    {
        public string Name = "";
        public string Description = "";
    }

    public class MapsSection
    {
        public string Purchased = "";
        public string Online = "";
    }

    public class GettingThereSection
    {
        // GettingThereOptions
        private List<GettingThereOption> gettingThereOptions = new List<GettingThereOption>();
        public GettingThereOption[] GettingThereOptions
        {
            get
            {
                return gettingThereOptions.ToArray();
            }
            set
            {
                gettingThereOptions = new List<GettingThereOption>(value);
            }
        }

        // Add GettingThereOption
        public void AddGettingThereOption(GettingThereOption gettingThereOption)
        {
            gettingThereOptions.Add(gettingThereOption);
        }
    }

    public class GettingThereOption
    {
        public string OptionName = "";
        public string OptionDescription = "";
    }

    public class PhotoGallerySection
    {
        // PhotosReferrenced
        private List<LocalImage> photosReferrenced = new List<LocalImage>();
        public LocalImage[] PhotosReferrenced
        {
            get
            {
                return photosReferrenced.ToArray();
            }
            set
            {
                photosReferrenced = new List<LocalImage>(value);
            }
        }

        // Add PhotosReferrenced
        public void AddPhotoReferrenced(LocalImage photoReferrenced)
        {
            photosReferrenced.Add(photoReferrenced);
        }
    }

    public class ConditionsSection
    {
        public string ConditionsText = "";
    }

    public class RelatedLinksSection
    {
        // Related Links
        private List<RelatedLink> relatedLinks = new List<RelatedLink>();
        public RelatedLink[] RelatedLinks
        {
            get
            {
                return relatedLinks.ToArray();
            }
            set
            {
                relatedLinks = new List<RelatedLink>(value);
            }
        }

        // Add Related Link
        public void AddRelatedLink(RelatedLink relatedLink)
        {
            relatedLinks.Add(relatedLink);
        }
    }

    public class RelatedLink
    {
        public string Name = "";
        public string URL = "";
    }

    public class LocalImage
    {
        public Guid InternalId = Guid.NewGuid();
        public string DisplayText = "";
        public string LocalPath = "";
        public string GalleryCaption = "";
        public string PageText = "";
    }
}
